%
%
%  Intensity calculation for Imaris
%
%
%  Installation:
%
%  - Copy this file into the XTensions folder in the Imaris installation directory
%  - You will find this function in the Image Processing menu
%
%    <CustomTools>
%      <Menu>
%       <Submenu name="McGill">
%        <Item name="Name channels according to colour" icon="Matlab" tooltip="Green goes to GFP and red goes to mCherry">
%          <Command>Matlab::MGNameChannels(%i)</Command>
%        </Item>
%       </Submenu>
%      </Menu>
%    </CustomTools>
%  
%
%  Description:
%
%   Rename the channels according to their colour since xuvtools can't set
%   channel names, but usually keeps the colour
%   Author: Hendrik Golzke, Patrik Rath
%   Last change: 2010-08-11

function MGNameChannels(aImarisApplicationID)

    % connect to Imaris Com interface
    if ~isa(aImarisApplicationID, 'COM.Imaris_Application')
      vImarisServer = actxserver('ImarisServer.Server');
      if ischar(aImarisApplicationID)
        aImarisApplicationID = round(str2double(aImarisApplicationID));
      end
      apImaris = vImarisServer.GetObject(aImarisApplicationID);
    else
      apImaris = aImarisApplicationID;
    end
    
    %Set undo point
    apImaris.DataSetPushUndo('Set channel names');

    %Load Data (Channels)
    dsChannels = apImaris.mDataSet;
    %Go through all channels and check the colours, opacity is ignored
    for iChannel=0:dsChannels.mSizeC-1
        [fR,fG,fB]=dsChannels.GetChannelColor(iChannel);
        if (isequal([1,0,0],[fR,fG,fB]))
            dsChannels.SetChannelName(iChannel,'mCherry');
        elseif (isequal([0,1,0],[fR,fG,fB]))
            dsChannels.SetChannelName(iChannel,'GFP');
        end;
    end;
    
end